import { defineStore } from 'pinia'
import type { LoginData, NavTab, UserInfo } from '@/types/user'
import { login, getUserInfo, logout } from '@/api/user'
import type { Token } from '@/types/data'
export const useUserInfoStore = defineStore('userInfo', {
  state: () => {
    // 用户信息
    return {
      tabList: [
        {
          title: '首页',
          path: '/home'
        }
      ] as NavTab[],
      collapse: false, // 菜单折叠状态
      token: {} as Token, // token
      user: {} as UserInfo // 用户信息
    }
  },
  actions: {
    // 登录获取token
    async login(loginForm: LoginData) {
      const token = await login(loginForm)
      this.token = token
    },
    // 获取用户信息
    async getUserInfo() {
      this.user = await getUserInfo()
    },
    // 切换菜单折叠状态
    switchFolding() {
      this.collapse = !this.collapse
    },
    // 退出登录
    async logout() {
      await logout()
      // 清除数据和状态
      this.$reset()
    }
  },
  persist: true
})
